<?php

/**
 * 涉外业务档案模型
 */
class Foreign_model extends CI_Model {
    const TN = "foreigns";
    
    /**
     * 添加时生成的档案号
     * @var type 
     */
    private $_file_no;
    
    public function get($id) {
        $query = $this->db->get_where(self::TN, array('f_id' => $id));
        
        return $query->first_row("array");
    }
    
    public function new_foreign() {
        return array(
            'file_no' => '保存时自动生成',
            'file_create_date' => date('Y-m-d'),
            'case_type' => FCT_SBSQ,
            'is_collected' => '否',
            'is_billed' => '否',
            'checks' => json_encode(array()),
            'mixture' => json_encode(array())
        );
    }
    
    public function insert($data) {
        $data['file_no'] = $this->_get_new_file_no();
        $data['total_fee'] = $data['custom_fee'] + $data['certification_fee'];
        $this->db->insert(self::TN, $data);
        $this->_file_no = $data['file_no'];
    }
    
    public function update($id, $data) {
        unset($data['file_no']);
        $data['total_fee'] = $data['custom_fee'] + $data['certification_fee'];
        $this->db->where('f_id', $id);
        $this->db->update(self::TN, $data);
    }
    
    public function delete($id) {
        $this->db->delete(self::TN, array('f_id' => $id));
    }
    
    public function file_no() {
        return $this->_file_no;
    }
    
    public function insert_id() {
        return $this->db->insert_id();
    }
    
    public function case_types() {
        return array(
            FCT_SBSQ,
            FCT_SBZR,
            FCT_SBBG,
            FCT_SBXZ,
            FCT_SBBZ,
            FCT_SBCX,
            FCT_MDLSQ,
            FCT_MDLHQZD,
            FCT_ECFF,
            FCT_TGSYZJ,
            FCT_SBFS,
            FCT_MDLFS,
            FCT_SBYY,
            FCT_SBYYDB,
            FCT_SBQT,
            FCT_FMZL,
            FCT_XXZL,
            FCT_WGZL,
            FCT_ZLBD,
            FCT_ZLNF,
            FCT_ZLSS,
            FCT_ZLFY,
            FCT_ZLQT,
        );
    }
    
    /**
     * 
     * @param type $conditions
     * @param type $per_page 默认0表示取所有记录
     * @param type $page
     * @return array
     */
    public function fetch_array($conditions = array(), $per_page = 0, $page = 0) {
        $this->_set_where($conditions);
        if ($per_page > 0) {
            $offset = ($page - 1) * $per_page;
            $this->db->limit($per_page, $offset);
        }
        $this->db->order_by('f_id', 'desc');

        return $this->db->get(self::TN)->result_array();
    }
    
    /**
     * 返回记录总数
     * param $conditions
     */
    public function fetch_count($conditions) {
        $this->_set_where($conditions);
        $this->db->from(self::TN);
        
        return $this->db->count_all_results();
    }
    
    /**
     * 返回query
     * @param type $conditions
     * @return type
     */
    public function export_query($conditions) {
        $this->db->select('file_no as `档案号`, file_create_date as `建档日期`, c_name as `申请人`, 
            tm_name as `商标/专利`, tm_class as `类别`, c_country as `指定国家`, case_type as `案件类型`, 
            agent as `代理人`, partner as `合作所`, total_fee as `总费用`, is_collected as `是否收款`');
        $this->_set_where($conditions);
        $this->db->order_by('f_id', 'desc');
        return $this->db->get(self::TN);
    }
    
    /**
     * 设置where
     * @param type $conditions
     */
    private function _set_where($conditions) {
        if (isset($conditions['c_name']) && $conditions['c_name'] != '') {
            $this->db->like('c_name', $conditions['c_name']);
        }
        if (isset($conditions['c_country']) && $conditions['c_country'] != '') {
            $this->db->like('c_country', $conditions['c_country']);
        }
        if (isset($conditions['file_no']) && $conditions['file_no'] != '') {
            $this->db->like('file_no', $conditions['file_no']);
        }
        if (isset($conditions['tm_name']) && $conditions['tm_name'] != '') {
            $this->db->like('tm_name', $conditions['tm_name']);
        }
        if (!empty($conditions['case_type'])) {
            $this->db->where_in('case_type', $conditions['case_type']);
        }
        if (!empty($conditions['agent'])) {
            $this->db->where_in('agent', $conditions['agent']);
        }
        if (!empty($conditions['partner'])) {
            $this->db->where_in('partner', $conditions['partner']);
        }

        if (!empty($conditions['file_create_date_from'])) {
            $this->db->where('file_create_date >=', $conditions['file_create_date_from']);
        }
        if (!empty($conditions['file_create_date_to'])) {
            $this->db->where('file_create_date <=', $conditions['file_create_date_to']);
        }
    }
    
    /**
     * 取下一个档案号
     * 规则，FF + 六位数字，从000001开始，自动增加
     */
    private function _get_new_file_no() {
        $this->db->select_max('file_no');
        $query = $this->db->get(self::TN);
        $row = $query->first_row('array');
        if (empty($row)) {
            return 'FF000001';
        } else {
            return 'FF' . str_pad(intval(substr($row['file_no'], 2)) + 1, 6, '0', STR_PAD_LEFT);
        }
    }
}